﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using DataLibrary.DataObject;

namespace DataLibrary
{
    [Serializable]
    public sealed class WebSiteContext
    {
        #region Private Fields
        private const string SessionCurrentUser = "Session:SessionCurrentUser";

        private static WebSiteContext webSiteContext;
        private WebSiteContext()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        static WebSiteContext()
        {
            webSiteContext = new WebSiteContext();
        }
        #endregion

        #region Public Satatic Fields
        public static WebSiteContext Current
        {
            get { return webSiteContext; }
        }

        public static int CurrentUserID
        {
            get
            {
                if (webSiteContext == null)
                    return int.MinValue;
                if (webSiteContext.CurrentUser == null)
                    return int.MinValue;

                return webSiteContext.CurrentUser.ID;
            }
        }

        public static int CurrentUserRoleID
        {
            get
            {
                if (webSiteContext == null)
                    return int.MinValue;
                if (webSiteContext.CurrentUser == null)
                    return int.MinValue;

                return webSiteContext.CurrentUser.RoleID;
            }
        }

        public static string CurrentUserName
        {
            get
            {
                if (webSiteContext == null)
                    return string.Empty;
                if (webSiteContext.CurrentUser == null)
                    return string.Empty;
                return webSiteContext.CurrentUser.UserName;
            }
        }

        #endregion

        #region Public APIs
        public bool ExistSystem()
        {
            if (Current.CurrentUser != null && Current.CurrentUser.ID > 0)
            {
                Current.CurrentUser = null;
                return true;
            }
            return false;
        }

        public void ClearSession()
        {
            HttpContext.Current.Session.Clear();
        }
        #endregion
        public DTUsersDO CurrentUser
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.Session != null)
                {
                    return HttpContext.Current.Session[SessionCurrentUser] as DTUsersDO;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                HttpContext.Current.Session[SessionCurrentUser] = value;
            }
        }

    }
}
